//
//  BaseVC.m
//  AlumniBook
//
//  Created by 喂！ on 2018/10/24.
//  Copyright © 2018年 well. All rights reserved.
//

#import "BaseVC.h"

@interface BaseVC ()

@end

@implementation BaseVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = true;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = false;
    self.view.backgroundColor = [UIColor colorWithRed:235/255.0f green:236/255.0f blue:242/255.0f alpha:1.0f];
    

    self.cusNavigationView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.cusNavigationView];
    [self.cusNavigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self.view);
        make.height.mas_equalTo(20 + self.navigationController.navigationBar.frame.size.height);
    }];
    

    self.leftButtonView.backgroundColor = [UIColor whiteColor];
    [self.cusNavigationView addSubview:self.leftButtonView];
    [self.leftButtonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(self.cusNavigationView);
        make.top.mas_equalTo(self.cusNavigationView).offset(20);
    }];
    

    self.rightButtonView.backgroundColor = [UIColor whiteColor];
    [self.cusNavigationView addSubview:self.rightButtonView];
    [self.rightButtonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(self.cusNavigationView);
        make.top.mas_equalTo(self.cusNavigationView).offset(20);
    }];
    

    self.titleLabel.backgroundColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:38 * SCREEN_SCALE];
    self.titleLabel.textColor = [UIColor colorWithRed:144/255.0f green:148/255.0f blue:158/255.0f alpha:1.0f];
    [self.cusNavigationView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.mas_equalTo(self.cusNavigationView);
        make.top.mas_equalTo(self.cusNavigationView).offset(20);
        make.left.mas_greaterThanOrEqualTo(self.leftButtonView.mas_right);
        make.right.mas_lessThanOrEqualTo(self.rightButtonView.mas_left);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor colorWithRed:144/255.0f green:148/255.0f blue:158/255.0f alpha:1.0f];
    [self.cusNavigationView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.cusNavigationView);
        make.height.mas_equalTo(SCREEN_SCALE);
    }];
    
    [self setLeftButton:nil];
}


-(void)setTitleName:(NSString *)name
{
    self.titleLabel.text = name;
}

-(void)setLeftButton:(NSArray *)buttonArr
{
    [self.leftButtonView removeAllSubView];
    if (buttonArr == nil || buttonArr.count == 0) {
        
        UIButton *backButton = [[UIButton alloc] init];
        [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
        [backButton setImage:[UIImage imageNamed:@"BackArrow"] forState:UIControlStateNormal];
        [backButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [backButton setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [self.leftButtonView addSubview:backButton];
        [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.right.mas_equalTo(self.leftButtonView);
            make.width.mas_equalTo(backButton.mas_height);
        }];
        
        return;
    }
    UIView *lastView;
    NSInteger arrCount = buttonArr.count;
    for (int i = 0;i<arrCount;i++) {
        UIButton *btn = [buttonArr objectAtIndex:i];
        [self.leftButtonView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(self.leftButtonView);
            make.width.mas_equalTo(btn.mas_height);
            
            if (i == 0) {
                make.left.mas_equalTo(self.leftButtonView);
            }
            else
            {
                make.left.mas_equalTo(lastView.mas_right);
            }
            
            if (i == arrCount - 1) {
                make.right.mas_equalTo(self.leftButtonView);
            }
        }];
        lastView = btn;
    }
}

-(void)setRightButton:(NSArray *)buttonArr
{
    self.rightButtonArr = [buttonArr mutableCopy];
    [self.rightButtonView removeAllSubView];
    UIView *lastView;
    NSInteger arrCount = buttonArr.count;
    for (int i = 0;i<arrCount;i++) {
        UIButton *btn = [buttonArr objectAtIndex:i];
        [self.rightButtonView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(self.rightButtonView);
            make.width.mas_equalTo(btn.mas_height);
            
            if (i == 0) {
                make.right.mas_equalTo(self.rightButtonView);
            }
            else
            {
                make.right.mas_equalTo(lastView.mas_left);
            }
            
            if (i == arrCount - 1) {
                make.left.mas_equalTo(self.rightButtonView);
            }
        }];
        lastView = btn;
    }
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:false];
}


#pragma mark 懒加载


-(NSMutableArray *)rightButtonArr
{
    if (!_rightButtonArr) {
        _rightButtonArr = [NSMutableArray array];
    }
    return _rightButtonArr;
}

-(UIView *)cusNavigationView
{
    if (!_cusNavigationView) {
        _cusNavigationView = [UIView new];
    }
    return _cusNavigationView;
}

-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
    }
    return _titleLabel;
}


-(UIView *)leftButtonView
{
    if (!_leftButtonView) {
        _leftButtonView = [UIView new];
    }
    return _leftButtonView;
}

-(UIView *)rightButtonView
{
    if (!_rightButtonView) {
        _rightButtonView = [UIView new];
    }
    return _rightButtonView;
}


@end
